/*
 * $RCSfile: OnlineUserThread.java,v $
 * $Revision: 1.1  $
 * $Date: 2012-8-10  $
 *
 * Copyright (C) 2005 Bettem, Inc. All rights reserved.
 *
 * This software is the proprietary information of Bettem, Inc.
 * Use is subject to license terms.
 */
package com.dzpk.gameserver.module.main;
/*
 * $RCSfile: OnlineUserThread.java,v $
 * $Revision: 1.1  $
 * $Date: 2012-8-10  $
 *
 * Copyright (C) 2005 Bettem, Inc. All rights reserved.
 *
 * This software is the proprietary information of Bettem, Inc.
 * Use is subject to license terms.
 */

import java.util.Iterator;

import com.dzpk.gameserver.module.main.room.Room;
import org.apache.log4j.Logger;


/**
 * 检测在线用户
 * <p>Title: OnlineUserThread</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * @author shenxing.ruan
 * @version 1.0
 */
public class OnlineUserThread implements Runnable {
    private int r_ = 0;
    public static Logger logger = Logger.getLogger(OnlineUserThread.class);
    private final static long V_TIME = 30 * 1000 - 10;
    private final static long SPACE_LOAD_TIME = 20 * 1000; //20S扫描一次在线列表 操作30秒不在线T掉

    public void run() {
        while (r_ == 0) {
            try {
                action();
                //暂停
                Thread.sleep(SPACE_LOAD_TIME);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private void action() {
        Iterator<UserInfo> it = Cache.getOnlineUserInfo().values().iterator();
        while (it.hasNext()) {
            UserInfo userInfo = it.next();
            if (System.currentTimeMillis() - userInfo.getOnlinetTime() > V_TIME) {
                Room room = Cache.getRoom(userInfo.getRoomId(), userInfo.getRoomPath());
                if (room != null) {
                    room.getRoomService().userTimeOut(userInfo);
                } else {
                    logger.error("出现了个空房间......业务逻辑有问题！！");
                }
                it.remove();
            }
        }
    }

    public void close() {
        r_ = -999;
    }

    public static void main(String[] args) {
        (new OnlineUserThread()).action();
    }
}

